package com.ht.shop.controller;

import com.alibaba.druid.util.StringUtils;
import com.ht.shop.exceptions.MyBaseException;
import com.ht.shop.pojo.TbUser;
import com.ht.shop.service.UserService;
import com.ht.shop.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

//@WebServlet("/users")
public class UserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");
        switch (method) {
            case "register":
                register(req,resp);
                break;
            case "login":
                login(req,resp);
                break;
            case "logOut":
                logOut(req,resp);
                break;
        }


    }

    private void logOut(HttpServletRequest req, HttpServletResponse resp) {
        req.getSession().invalidate();
        try {
            resp.sendRedirect("/index.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 登录
     * @param req
     * @param resp
     */
    private void login(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        String savecode = (String)req.getSession().getAttribute("code");
        if (StringUtils.isEmpty(code)|| !code.equalsIgnoreCase(savecode)){

            resp.setContentType("text/html;charset=utf-8");
            try {
                resp.getWriter().write("验证码错误");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        try {
            TbUser user = userService.login(username, password);
            if (user == null) {
                resp.setContentType("text/html;charset=utf-8");
                resp.getWriter().write("用户名或密码错误");
                return;
            }
            req.getSession().setAttribute("loginUser",user);
            resp.sendRedirect("/index.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            try {
                resp.sendRedirect("/error/error.jsp");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 注册
     * @param req
     * @param resp
     * @throws IOException
     */
    private void register(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            TbUser user = new TbUser();
            Map<String, String[]> parameterMap = req.getParameterMap();
            BeanUtils.populate(user,parameterMap);

            String repeatPassword = req.getParameter("repeatpassword");
            if (StringUtils.isEmpty(repeatPassword)||!repeatPassword.equals(user.getPassword())) {
                req.setAttribute("meg","两次输入的密码不一致");
                req.getRequestDispatcher("/error/fail.jsp").forward(req, resp);
                return;
            }
            userService.addUser(user);
            resp.sendRedirect("/registerSuccess.jsp");
        } catch (MyBaseException e) {
            e.printStackTrace();
            req.getSession().setAttribute("msg",e.getMessage());
            resp.sendRedirect("/error/fail.jsp");

        } catch (Exception e) {
            e.printStackTrace();
            resp.sendRedirect("/error/fail.jsp");
        }
    }
}
